using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SkillListDisplay : MonoBehaviour {
	
	public GameObject itemDisplayTemplate;
	
	List<GameObject> itemDisplays;
	
	public UITable table;
	
	public bool toggled = false;
	
	Skill selectedSkill;

	// Use this for initialization
	void Start () {
		
		itemDisplayTemplate.SetActiveRecursively(false);
		
		itemDisplays = new List<GameObject>();
		
		UIButtonMessage message = gameObject.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "ToggleSkillDisplays";
		
		table.repositionNow = true;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public void ToggleSkillDisplays()
	{
		if(!toggled)
		{
			itemDisplayTemplate.SetActiveRecursively(true);
			
			List<Skill> sortedList = SheetManager.activeCharacter.skills;
			sortedList.Sort((w1,w2) => w1.name.CompareTo(w2.name));
			
			GameObject go = new GameObject();
			go.transform.parent = transform.parent;
			go.transform.localPosition = Vector3.zero;
			go.transform.localScale = Vector3.one;
			go.name = itemDisplayTemplate.name;
			itemDisplays.Add(go);
			
			go = NGUITools.AddButton(go, 24, "Add Skill  (" + SheetManager.activeCharacter.ranksUsed + "/" + SheetManager.activeCharacter.totalRanks + " Ranks)");
			UIButtonMessage message = go.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "AddToSkillList";
			go.transform.localPosition = Vector3.right * ((BoxCollider)go.collider).size.x/2;
			
			foreach(Skill skill in sortedList)
			{
				go = Instantiate(itemDisplayTemplate) as GameObject;
				go.transform.parent = transform.parent;
				go.transform.localPosition = itemDisplayTemplate.transform.localPosition;
				go.transform.localScale = itemDisplayTemplate.transform.localScale;
				go.name += skill.name;
				
				SkillListDisplayTemplate template = go.GetComponent<SkillListDisplayTemplate>();
				template.skillName.text = skill.name;
				template.abilityTotal.text = skill.statBonus.ToString();
				template.abilityName.text = skill.stat.Substring(0,3).ToUpper();
				template.bonusTotal.text = skill.totalbonus.ToString();
				template.rankTotal.text = skill.rank.ToString();
				template.totalValue.text = skill.TotalValue.ToString();
				
				template.skill = skill;
				
				message = go.AddComponent<UIButtonMessage>();
				message.target = gameObject;
				message.functionName = "DisplaySkillOptions";
				
				message = template.LessButton.AddComponent<UIButtonMessage>();
				message.target = gameObject;
				message.functionName = "ReduceQuantity";
				
				message = template.MoreButton.AddComponent<UIButtonMessage>();
				message.target = gameObject;
				message.functionName = "IncreaseQuantity";
				
				itemDisplays.Add(go);
			}
			
			itemDisplayTemplate.SetActiveRecursively(false); 
			
			table.repositionNow = true;
			toggled = true;
		}
		else
		{
			foreach(GameObject go in itemDisplays)
			{
				Destroy(go);
			}
			itemDisplays.Clear();
			table.repositionNow = true;
			toggled = false;
		}
	}
	
	IEnumerator IncreaseQuantity(GameObject target)
	{
		SkillListDisplayTemplate template = target.transform.parent.GetComponent<SkillListDisplayTemplate>();
		if(template.skill.rank < SheetManager.activeCharacter.Level)
		{
			bool doIt = true;
			
			if(SheetManager.activeCharacter.totalRanks <= SheetManager.activeCharacter.ranksUsed)
			{
				List<bool> output = new List<bool>{false};
				
				yield return StartCoroutine(HelperFunctions.ConfirmationWindow("This will add more ranks than your character legally is allowed. Continue anyway?", output));
				if(!output[0])
					doIt = false;
			}
			
			if(doIt)
			{
				template.skill.rank++;
				template.abilityTotal.text = template.skill.statBonus.ToString();
				template.bonusTotal.text = template.skill.totalbonus.ToString();
				template.rankTotal.text = template.skill.rank.ToString();
				template.totalValue.text = template.skill.TotalValue.ToString();
				
				SheetManager.SaveActiveCharacter(false);
			}
		}
		
		yield break;
	}
	
	void ReduceQuantity(GameObject target)
	{
		SkillListDisplayTemplate template = target.transform.parent.GetComponent<SkillListDisplayTemplate>();
		
		selectedSkill = template.skill;
		
		if(template.skill.rank > 0)
		{
			template.skill.rank--;
			template.abilityTotal.text = template.skill.statBonus.ToString();
			template.bonusTotal.text = template.skill.totalbonus.ToString();
			template.rankTotal.text = template.skill.rank.ToString();
			template.totalValue.text = template.skill.TotalValue.ToString();
			
			SheetManager.SaveActiveCharacter(false);
		}
		else
		{
			StartCoroutine(RemoveSkill());
		}
	}
	
	IEnumerator RemoveSkill()
	{
		List<bool> outBools = new List<bool>{false};
		
		yield return StartCoroutine(HelperFunctions.ConfirmationWindow("Are you sure you want to remove " + selectedSkill.name + " from your character sheet?", outBools));
		
		if(outBools[0])
		{
			SheetManager.activeCharacter.skills.Remove(selectedSkill);
			
			ToggleSkillDisplays();
			ToggleSkillDisplays();
			
			SheetManager.SaveActiveCharacter(false);
		}
	}
	
	void AddToSkillList()
	{
		BrowserManager.CreateSkillBrowser(gameObject, "AddSkill", new List<string>());
	}
	
	void AddSkill(List<string> addedSkills)
	{
		foreach(string sn in addedSkills)
		{	
			if(SheetManager.activeCharacter.skills.Find(s => s.name == sn) == null)
			{
				Skill s = SkillManager.Load(sn);
				if(s != null)
					SheetManager.activeCharacter.skills.Add(s);
			}
		}
		
		SheetManager.SaveActiveCharacter(false);
		ToggleSkillDisplays();
		ToggleSkillDisplays();
	}
	
	void SkillRoll()
	{		
		selectedSkill.RollSkill();
	}
	
	IEnumerator DisplaySkillOptions(GameObject target)
	{
		selectedSkill = target.GetComponent<SkillListDisplayTemplate>().skill;
		
		List<string> options = new List<string>{"View Skill Info","Remove Skill", "Roll Skill Check"};
		
		List<SetterWindowDescription> descVar = new List<SetterWindowDescription>
		{
			new SetterWindowDescription("", "Action", false, options)
		};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow("Options", descVar));
		
		switch(descVar[0].outVariable)
		{
			case "View Skill Info":
				SkillInfo();
				break;
			case "Remove Skill":
				StartCoroutine(RemoveSkill());
				break;
			case "Roll Skill Check":
				SkillRoll();
				break;
		}
	}
	
	void SkillInfo()
	{
		GameObject g = Instantiate(GameReference.reference.CreateSkillWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		BrowserManager.currentNewTransform = g.transform;
		g.transform.localPosition = BrowserManager.CurrentIndex;
		
		CreateSkillWindow s = g.GetComponent<CreateSkillWindow>();
		s.target = gameObject;
		s.functionName = "Do Nothing";
		s.LoadSkillDisplay(selectedSkill.name, true);
	}
}
